package com.hddz.comlib.util;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;

import com.hddz.comlib.R;

public class CommandFactroy {
	// private static Decimalformat mdfStation = new Decimalformat("#00000000");

	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmm");

	public static String getReadworstate() {
		return "readworstate";
	}

	public static String getQureyRealTime(int station) {
		return String.format("%08d#ZQ111#", station);
	}

	public static String getQureyRealTime(int station, String zx, String fx, String sj, String s1, String s2) {
		List<Integer> p = Arrays.asList(0, 0, 0);
		List<Integer> a = Arrays.asList(0, 0);
		getChanle(zx, p, a);
		getChanle(fx, p, a);
		getChanle(sj, p, a);
		getChanle(s1, p, a);
		getChanle(s2, p, a);
		return String.format("%08d#ZQ%s#", station, (p.get(0) + a.get(0)) + "" + (p.get(1) + a.get(1)) + "" + (p.get(2)));
	}

	public static String getHistroy(int station, Date dt) {
		return String.format("%08d#Z%s#", station, dateFormat.format(dt));
	}

	/**
	 * 历史数据包
	 * 
	 * @param station
	 * @param dt
	 *            起始时间
	 * @param count
	 *            查询个数
	 * @param interval
	 *            0：不保存;1：15分钟；2：30分钟
	 *            3：1小时；4：2小时;5：4小时；6：6小时；7：12小时；8：24小时C：1分钟;D：5分钟；E：10分钟；
	 * @return
	 */
	public static String getHistroyMore(int station, Date dt, int count, String interval) {
		if (count > 999) {
			count = 999;// "%08d#Z%s%03d%sQ%s#"
		}
		return String.format("%08d#Z%s%03d%sQ611#", station, dateFormat.format(dt), count, interval);
	}

	public static Date getStartDate(Date dt, String interval) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		if (interval.equals("1")) {
			cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE) / 15) * 15);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.MINUTE, 15 * (cal.get(Calendar.MINUTE) % 15 > 0 ? 1 : 0));
		} else if (interval.equals("2")) {
			cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE) / 30) * 30);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.MINUTE, 30 * (cal.get(Calendar.MINUTE) % 30 > 0 ? 1 : 0));
		} else if (interval.equals("3")) {
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
		} else if (interval.equals("4")) {
			cal.set(Calendar.HOUR_OF_DAY, (cal.get(Calendar.HOUR_OF_DAY) / 2) * 2);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.HOUR_OF_DAY, 2 * (cal.get(Calendar.HOUR_OF_DAY) % 2 > 0 ? 1 : 0));
		} else if (interval.equals("5")) {
			cal.set(Calendar.HOUR_OF_DAY, (cal.get(Calendar.HOUR_OF_DAY) / 4) * 4);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.HOUR_OF_DAY, 4 * (cal.get(Calendar.HOUR_OF_DAY) % 4 > 0 ? 1 : 0));
		} else if (interval.equals("6")) {
			cal.set(Calendar.HOUR_OF_DAY, (cal.get(Calendar.HOUR_OF_DAY) / 6) * 6);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.HOUR_OF_DAY, 6 * (cal.get(Calendar.HOUR_OF_DAY) % 6 > 0 ? 1 : 0));
		} else if (interval.equals("7")) {
			cal.set(Calendar.HOUR_OF_DAY, (cal.get(Calendar.HOUR_OF_DAY) / 12) * 12);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.HOUR_OF_DAY, 12 * (cal.get(Calendar.HOUR_OF_DAY) % 12 > 0 ? 1 : 0));
		} else if (interval.equals("8")) {
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
		} else if (interval.equals("C")) {
			cal.set(Calendar.SECOND, 0);
		} else if (interval.equals("D")) {
			cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE) / 5) * 5);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.MINUTE, 5 * (cal.get(Calendar.MINUTE) % 5 > 0 ? 1 : 0));
		} else if (interval.equals("E")) {
			cal.set(Calendar.MINUTE, (cal.get(Calendar.MINUTE) / 10) * 10);
			cal.set(Calendar.SECOND, 0);
			cal.add(Calendar.MINUTE, 10 * (cal.get(Calendar.MINUTE) % 10 > 0 ? 1 : 0));
		}
		return cal.getTime();
	}

	/**
	 * 历史数据包
	 * 
	 * @param station
	 *            站号
	 * @param zx
	 *            正向
	 * @param fx
	 *            反向
	 * @param sj
	 *            实际
	 * @param s1
	 *            模拟一
	 * @param s2
	 *            模拟二
	 * @param dt
	 *            起始时间
	 * @param count
	 *            查询个数
	 * @param interval
	 *            0：不保存;1：15分钟；2：30分钟
	 *            3：1小时；4：2小时;5：4小时；6：6小时；7：12小时；8：24小时C：1分钟;D：5分钟；E：10分钟；
	 * @return
	 */
	public static String getHistroyMore(int station, String zx, String fx, String sj, String s1, String s2, Date dt, int count, String interval) {
		List<Integer> p = Arrays.asList(0, 0, 0);
		List<Integer> a = Arrays.asList(0, 0);
		if (count > 999) {
			count = 999;
		}
		getChanle(zx, p, a);
		getChanle(fx, p, a);
		getChanle(sj, p, a);
		getChanle(s1, p, a);
		getChanle(s2, p, a);

		return String.format("%08d#Z%s%03d%sQ%s#", station, dateFormat.format(getStartDate(dt, interval)), count, interval, (p.get(0) + a.get(0)) + "" + (p.get(1) + a.get(1)) + "" + (p.get(2)));
	}

	private static void getChanle(String chanle, List<Integer> p, List<Integer> a) {
		if (chanle != null && chanle.length() >= 2) {
			String c = chanle.toLowerCase();
			String n = c.substring(0, 1);
			int i = Integer.parseInt(c.substring(1, 2));
			if (n.equals("p")) {
				if (i > 0 && i <= 3) {
					p.set(i - 1, 1);
				}
			} else if (n.equals("a")) {
				if (i > 0 && i <= 2) {
					a.set(i - 1, 5);
				}
			}
		}
	}

	public static String getVersion(int station) {
		return String.format("%08d#Y#", station);
	}

	public static String getParameter(int station, String p) {
		return String.format("%08d#%s?#", station, p);
	}

	public static String getParameter2(int station, String p) {
		return String.format("%08d#%s#", station, p);
	}

	public static String getInitParameter(int station, String p) {
		return String.format("%08d#%s.#", station, p);
	}

	public static String getReBoot(int station) {
		return String.format("%08d#XR*#", station);
	}

	public static String getRebuildAllParams(int station) {
		return String.format("%08d#X*#", station);
	}

	/**
	 * 设置站号
	 * 
	 * @param station
	 * @param stationID
	 * @return
	 */
	public static String setStationID(int station, int stationID) {
		return String.format("%08d#A:%08d#", station, stationID);
	}

	/**
	 * 设置时间
	 * 
	 * @param station
	 * @param dt
	 * @return
	 */
	public static String setTime(int station, Date dt) {
		return String.format("%08d#C:%s#", station, dateFormat.format(dt));
	}

	/**
	 * @param station
	 * @param interval
	 *            0：不保存; 1：15分钟; 2：30分钟；3：1小时；4：2小时； C：1分钟； D：5分钟；
	 *            E：10分钟；</param>
	 * @return
	 */
	public static String setSaveInterval(int station, String interval) {
		return String.format("%08d#D:%s#", station, interval);
	}

	/**
	 * @param station
	 * @param interval
	 *            0：不发送 1：10秒； 2：20秒； 3：30秒； 4：1分钟；
	 * @return
	 */
	public static String setSendInterval(int station, String interval) {
		return String.format("%08d#E1:%s#", station, interval);
	}

	/**
	 * 
	 * @param station
	 * @param show
	 *            x=0：系统休眠时，关液晶显示；x=1：系统休眠时，液晶正常显示；
	 * @return
	 */
	public static String setLCM(int station, int show) {
		return String.format("%08d#J:%d#", station, show);
	}

	/**
	 * 模拟量量程设置
	 * 
	 * @param station
	 * @param value
	 * @return
	 */
	public static String setMValue(int station, float value) {
		return String.format("%08d#M1:%s#", station, String.format("%f", value).subSequence(0, 5));
	}

	/**
	 * 当前脉冲底度设置
	 * 
	 * @param station
	 * @param chanelIndex
	 * @param value
	 * @return
	 */
	public static String setPValue(int station, int chanelIndex, int value) {
		return String.format("%08d#P%1d:%08d#", station, chanelIndex, value);
	}

	public static String set8Chanle(int station, int value) {
		return String.format("%08d#Q:%03d#", station, value);
	}

	public static String get3Chanle(int station) {
		return String.format("%08d#Q?#", station);
	}

	public static String setSValue(int station, float value) {
		return String.format("%08d#S1:%s#", station, String.format("%f", value).subSequence(0, 5));
	}

	public static String setT(int station, int value) {
		return String.format("%08d#T1:%02d#", station, value);
	}

	public static String getT(int station) {
		return String.format("%08d#T1?#", station);
	}

	public static String setVValue(int station, int value) {
		return String.format("%08d#V:%02d#", station, value);
	}

	public static String setFCValue(int station, float value) {
		return String.format("%08d#FC:%s#", station, String.format("%f", value).subSequence(0, 5));
	}

	/**
	 * 状态回复等待周期设置 - BT（默认：50mS） 有效唤醒次数设置 - BC（默认：3）
	 * 
	 * @param station
	 * @param value
	 * @return
	 */
	public static String setBCValue(int station, int value) {
		return String.format("%08d#BC:%02d#", station, value);
	}

	/**
	 * @param station
	 * @param value
	 * @return
	 */
	public static String setBTValue(int station, int value) {
		return String.format("%08d#BT:%02d#", station, value);
	}

	/**
	 * 数据包发送间隔时间设置 - BK（默认：50mS）
	 * 
	 * @param station
	 * @param value
	 * @return
	 */
	public static String setBKValue(int station, int value) {
		return String.format("%08d#BK:%02d#", station, value);
	}

	/**
	 * 无线模块通讯速率
	 * 
	 * @param value
	 * @return
	 */
	public static String setBaudRate(int value) {
		return String.format("666666#BA:%d#", value);
	}

	/**
	 * 无线模块通讯频率
	 * 
	 * @param value
	 * @return
	 */
	public static String setFrequency(int value) {
		return String.format("666666#FR:%d#", value);
	}

	/**
	 * 无线模块发射接收功率
	 * 
	 * @param value
	 * @return
	 */
	public static String setCapy(int value) {
		return String.format("666666#PA:%d#", value);
	}

	/**
	 * 查询电量报文
	 * 
	 * @return
	 */
	public static byte[] getPowerFrom433MHZ() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x01, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 波特速率
	 * 
	 * @return
	 */
	public static byte[] getBaudRateFrom433MHZ() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x02, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 频率
	 * 
	 * @return
	 */
	public static byte[] getFrequencyFrom433MHZ() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x03, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 功率
	 * 
	 * @return
	 */
	public static byte[] getCapyFrom433MHZ() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x04, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 设置波特速率
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] setBaudRateFrom433MHZ(int value) {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x02, 0x01, (byte) value, 0x0D, 0x0A };
	}

	/**
	 * 设置频率 00:431Khz;01:433Khz;02:435Khz；
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] setFrequencyFromMHZ(int value) {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x03, 0x01, (byte) value, 0x0D, 0x0A };
	}

	/**
	 * 设置功率
	 * 
	 * @param value
	 * @return
	 */
	public static byte[] setCapyFrom433MHZ(int value) {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x04, 0x01, (byte) value, 0x0D, 0x0A };
	}

	/**
	 * 初始化
	 * 
	 * @return
	 */
	public static byte[] setInitFrom433MHZ() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x06, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 获取唤醒命令
	 * 
	 * @return
	 */
	public static byte[] getWeek() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x04, 0x00, 0x0D, 0x0A };
	}

	/**
	 * 获取唤醒命令（只唤醒不取数）
	 * 
	 * @return
	 */
	public static byte[] getWeekSample() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x02, 0x07, 0x03, 0x0D, 0x0A };
	}

	/**
	 * 获取睡眠命令
	 * 
	 * @return
	 */
	public static String getSleep() {
		return "00314016#G:1#";
	}

	/**
	 * 开启GPS
	 * 
	 * @return
	 */
	public static byte[] getOpenGPS() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x05, 0x01, 0X02, 0x0D, 0x0A };
	}

	/**
	 * 关闭GPS
	 * 
	 * @return
	 */
	public static byte[] getCloseGPS() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x05, 0x01, 0x01, 0x0D, 0x0A };
	}

	/**
	 * 使无线模块休眠
	 * 
	 * @return
	 */
	public static byte[] getCloseNFC_R() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x07, 0x01, 0x01, 0x0D, 0x0A };
	}

	/**
	 * 退出休眠
	 * 
	 * @return
	 */
	public static byte[] getOpenNFC_R() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x07, 0x01, 0x02, 0x0D, 0x0A };
	}

	/**
	 * 关闭RFID
	 * 
	 * @return
	 */
	public static byte[] getCloseRFID() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x08, 0x01, 0x01, 0x0D, 0x0A };
	}

	/**
	 * 开启RFID
	 * 
	 * @return
	 */
	public static byte[] getOpenRFID() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x08, 0x01, 0x02, 0x0D, 0x0A };
	}

	/**
	 * 扫描RFID
	 * 
	 * @return
	 */
	public static byte[] getScanRFID() {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x09, 0x01, 0x01, 0x0D, 0x0A };
	}

	/**
	 * 控制NFC-R发送无线唤醒帧的时长指令
	 * 
	 * @param value
	 *            控制值
	 * @return
	 */
	public static byte[] setSendWeekFrequency(byte value) {
		return new byte[] { (byte) 0xDD, (byte) 0xDD, 0x03, 0x0A, 0x01, value, 0x0D, 0x0A };
	}

	public static String getHydrant(int station, int startindex, int count) {
		return String.format("%08d#B%05d%05d#", station, startindex, count);
	}

	public static int getMinuteByCode(String code) {
		if (code.equals("1")) {
			return 15;
		} else if (code.equals("2")) {
			return 30;
		} else if (code.equals("3")) {
			return 60;
		} else if (code.equals("4")) {
			return 120;
		} else if (code.equals("5")) {
			return 240;
		} else if (code.equals("6")) {
			return 360;
		} else if (code.equals("7")) {
			return 720;
		} else if (code.equals("48")) {
			return 1440;
		} else if (code.equals("C")) {
			return 1;
		} else if (code.equals("D")) {
			return 5;
		} else if (code.equals("E")) {
			return 10;
		} else {
			return 0;
		}
	}

	public static String getNFCError(Context context, String error) {
		if (error.equals("001")) {
			return context.getResources().getString(R.string.lbl_nfc_error_001);
		} else if (error.equals("002")) {
			return context.getResources().getString(R.string.lbl_nfc_error_002);
		} else if (error.equals("003")) {
			return context.getResources().getString(R.string.lbl_nfc_error_003);
		} else if (error.equals("004")) {
			return context.getResources().getString(R.string.lbl_nfc_error_004);
		}
		return "";
	}
}
